// pages/game/win.js
const TRADIS = require('../../tradis/tradis.js');
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    fee:0,
    isfz:false,
    pageIn: 'detail',
    userInfo:{},
    music_over:{},
    isHBgl:false,
    islevelUp: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      fee: options.fee,
      // grade: JSON.parse(options.grade),
    })
    // if(this.data.grade.id!=undefined){
    //   this.setData({
    //     islevelUp:true,
    //   })
    // }
    TRADIS.tradis(this);
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    })
    this.data.music_over = wx.createInnerAudioContext();
    this.data.music_over.autoplay = false;
    this.data.music_over.src = '/music/music_over.mp3';
    this.data.music_over.play();
    wx.hideShareMenu({
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.data.music_over.stop();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var imageUrl='';
    var title ='无敌是多么，多么寂寞！', path = 'pages/index/index';
    if (!this.data.islevelUp){
      imageUrl = app.getShareHBIMG();
      title = this.data.userInfo.nickName + '给你发红包了', path = 'pages/index/index?shareid=' + this.data.id;
    }
    return {
      title: title,
      imageUrl: imageUrl,
      path: path,
      success: (res) => {
        wx.showToast({
          title: '分享成功',
          icon: 'none',
        })
        if (!this.data.islevelUp) {
          this.setData({
            isHBgl: true,
          });
        }
        else{
          this.cancel();
        }
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          icon: 'none',
        })
      }

    }

  },
  closeHBgl: function () {
    this.setData({
      isHBgl: false,
    });
  },
  gamestart: function (e) {
    this.setData({
      isHBgl: false,
    });
    let token = wx.getStorageSync('token');
    if (token) {
      console.log(e.detail.formId);
      var _this = this;
        wx.request({
          url: app.globalData.api + '/Users/formId.html&token=' + wx.getStorageSync('token') + '&formId=' + e.detail.formId,
          header: {
            v: 2,
          },
          success: function (res) {
          }
        });
        wx.redirectTo({
          url: '/pages/game/play',
        })
    } else {
      wx.navigateTo({
        url: '/pages/auth/getuserinfo',
      })
    }


  },
  openHB:function(){
    this.setData({
      isfz: true,
    })
    setTimeout(() => {
      wx.redirectTo({
        url: '/pages/hongbao/detail?fee=' + this.data.fee +'&bonus_id='+this.data.id,
      })
    }, 1500)

  },
  cancel:function(){
    this.setData({
      islevelUp:false,
    })
  }
})